// mobile bind
// ============

define(['zepto', 'sui'], function($){
  return {
    init: function(data) {
      this.data = data;
      this.initPage();
      this.bindRuleModal();
    },
    
    initPage: function() {
      
      var that = this;
      
      $(document).on('click', '.confirm-target', function(e) {
    	  var redirect = $(e.currentTarget).data('redirect');
        $.confirm('您有需要填写的目标数据未填写，是否进入录入页面?',
          function() {
            var url =  window.__CTX__.root + "/profile/courses/" + that.data.roundId + "/targets/create?redirect=" + redirect;
            that.confirmTarget(true, url);
          },
          function() {
        	  var url = redirect;
        	  that.confirmTarget(false, url);
          }
        );
      });
      
    },
    
    confirmTarget: function(confirm, redirect) {
    	var that = this;
    	$.ajax({
            type: 'POST',
            url: window.__CTX__.root + "/profile/courses/" + that.data.roundId + "/confirm-target",
            data: {'confirm' : confirm},
            success: function(result) {
            	setTimeout(function() {
                    window.location.href = redirect;
                  }, 1000)
            },
            failure: function(result) {
            	$.toast(result.message || '填写学员目标失败')
            }
          })
    },
    
    bindRuleModal: function() {
      $(document).on('click','.open-rules', function () {
        $.modal({
          extraClass: 'rule-modal',
          text:  '<div style="margin-bottom: 1.2rem;">' +
                   '<h4 class="text-center" style="color: #777; margin-bottom: .5rem;">约课须知</h4>' +
                   '<p style="margin: 0px; font-size: .7rem; color: #999;">1：普通用户每日只能预约一次课程。</p>' +
                   '<p style="margin: 0px; font-size: .7rem; color: #999;">2：普通用户超过两次课程逾期（未签到或未去上课）将变为失信用户。</p>' +
                   '<p style="margin: 0px; font-size: .7rem; color: #999;">3：失信用户每周只能预约一节课程。</p>' +
                   '<p style="margin: 0px; font-size: .7rem; color: #999;">4：失信用户最近连续签到两天后，将变回普通用户。</p>' +
                 '</div>',
          buttons: [
            {
              text: '朕知道了',
              close: true
            }
          ]
        })
      });
    }
  }
})

